package com.jeecms.wicool.action.indexPublish;

import com.google.common.primitives.Ints;
import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.cms.web.CmsUtils;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.cms.CmsInfo;
import com.jeecms.wicool.entity.groupBuy.GroupBuy;
import com.jeecms.wicool.entity.killBuy.KillBuy;
import com.jeecms.wicool.entity.product.Product;
import com.jeecms.wicool.entity.product.VIPProduct;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.cms.CmsInfoMng;
import com.jeecms.wicool.manager.groupBuy.GroupBuyMng;
import com.jeecms.wicool.manager.killBuy.KillBuyMng;
import com.jeecms.wicool.manager.product.ProductMng;
import com.jeecms.wicool.manager.product.VIPProductMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

import static com.jeecms.common.page.SimplePage.cpn;

/**
 * Created by cuiyanlei on 15/6/21.
 */
@RequestMapping("/indexPublish")
@Controller

public class IndexPublish {

    private static final String TITLE_TEMP = "%s--首页发布";

    private static final String CMS_PATH_PRODUCT_TEMP = "%s/skx/product/detail.do?productId=%s";
    private static final String CMS_PATH_GROUP_BUY_TEMP = "%s/skx/groupbuy/detail.do?id=%s";
    private static final String CMS_PATH_KILL_BUY_TEMP = "%s/skx/killbuy/detail.do?id=%s";
    private static final String CMS_PATH_VIP_PRODUCT_TEMP = "%s/skx/product/detail.do?productId=%s";


    @RequestMapping("v_add.do")
    public String add(Integer from, HttpServletRequest request, ModelMap model) {
        Integer relationId = Ints.tryParse(request.getParameter("relationId"));
        String titleName = null;
        String cmsPath = null;

        switch (from) {
            case 1:
                Product product = productMng.findById(relationId);
                titleName = String.format(TITLE_TEMP, product.getProductName() + "--商品");
                cmsPath = String.format(CMS_PATH_PRODUCT_TEMP, product.getShop().getDetailPageURL(), product.getId());
                break;
            case 2:
                GroupBuy groupBuy = groupBuyMng.findById(relationId);
                titleName = String.format(TITLE_TEMP, groupBuy.getProductName() + "--拼单");
                cmsPath = String.format(CMS_PATH_GROUP_BUY_TEMP, groupBuy.getProduct().getShop().getDetailPageURL(), groupBuy.getId());
                break;
            case 3:
                KillBuy killBuy = killBuyMng.findById(relationId);
                titleName = String.format(TITLE_TEMP, killBuy.getProductName() + "--秒杀");
                cmsPath = String.format(CMS_PATH_KILL_BUY_TEMP, killBuy.getProduct().getShop().getDetailPageURL(), killBuy.getId());
                break;
            case 4:
            	 Product vipProduct = productMng.findById(relationId);
                titleName = String.format(TITLE_TEMP, vipProduct.getProductName() + "--VIP商品");
                cmsPath = String.format(CMS_PATH_VIP_PRODUCT_TEMP, vipProduct.getShop().getDetailPageURL(), vipProduct.getId());
                break;
        }
        model.addAttribute("publisherName", CmsUtils.getUser(request).getUsername());
        model.addAttribute("titleName", titleName);
        model.addAttribute("relationId", relationId);
        model.addAttribute("from", from);
        model.addAttribute("publishTime", DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
        model.addAttribute("cmsPath", cmsPath);
        List<ShopInfo> listShop = shopMng.getAll(CmsUtils.getUser(request));
        model.addAttribute("listShop", listShop);
        return "index_publish/add_index";
    }

    @RequestMapping("v_list.do")
    public String list(String title, Integer pageNo, Integer publishType, HttpServletRequest request, ModelMap model) {
        Short type = null;
        if (null != publishType) {
            type = publishType.shortValue();
        }
        Pagination pagination = cmsInfoMng.getPage(title, cpn(pageNo), CookieUtils.getPageSize(request), type);
        List<CmsInfo> elements = (List<CmsInfo>) pagination.getList();
        model.addAttribute("pagination", pagination);
        model.addAttribute("elements", elements);
        return "index_publish/list_index";
    }

    @RequestMapping("o_delete.do")
    public String delete(Integer[] ids) {
        for (Integer id : ids) {
            cmsInfoMng.deleteById(id);
        }
        return "redirect:v_list.do";
    }


    @RequestMapping("v_edit.do")
    public String edit(Integer id, HttpServletRequest request, ModelMap model) {
        CmsInfo bean = cmsInfoMng.findById(id);
        List<ShopInfo> listShop = shopMng.getAll(CmsUtils.getUser(request));
        model.addAttribute("cmsInfo", bean);
        model.addAttribute("listShop", listShop);

        return "index_publish/edit_index";
    }

    @RequestMapping("o_update.do")
    public String update(Integer id, String title, Integer shopId, String titlePicURL, Integer publishType, Integer orderNum, String cmsPath, String description, ModelMap model, HttpServletRequest request) {
        CmsInfo bean = cmsInfoMng.findById(id);
        ShopInfo shop = shopMng.findById(shopId);
        if (null != bean) {
            bean.setTitle(title);
            bean.setShop(shop);
            bean.setTitleImage(titlePicURL);
            bean.setIndexType(publishType);
            bean.setOrderNum(orderNum);
            bean.setCmsPath(cmsPath);
            bean.setSimpleContent(description);
            bean.setUpdateTime(new Timestamp(DateTime.now().getMillis()));
            cmsInfoMng.update(bean, shopId);
        }
        model.addAttribute("message", "global.success");
        return edit(id, request, model);
    }


    @RequestMapping("o_save.do")
    public String save(String title, Integer from, Integer shopId, String titlePicURL, String publishTime, Integer publishType, Integer orderNum, String cmsPath, String description, HttpServletRequest request) {
        ShopInfo shop = shopMng.findById(shopId);
        CmsUser user = CmsUtils.getUser(request);
        CmsInfo cmsInfo = new CmsInfo(null, shop, title, description, cmsPath, new Short("1"), orderNum, user, new Timestamp(DateTime.parse(publishTime, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).getMillis()), new Timestamp(DateTime.now().getMillis()), titlePicURL, null);
        cmsInfo.setIndexType(publishType);
        cmsInfo.setStatus((short) -1);
        cmsInfoMng.save(cmsInfo, shopId);
        String redirectURL = null;
        switch (from) {
            case 1:
                redirectURL = "redirect:../product/v_list.do";
                break;
            case 2:
                redirectURL = "redirect:../groupBuy/v_list.do";
                break;
            case 3:
                redirectURL = "redirect:../killBuy/v_list.do";
                break;
            case 4:
                redirectURL = "redirect:../vipProduct/v_list.do";
                break;
        }
        return redirectURL;
    }

    @RequestMapping("o_changeStatus.do")
    public String changeStatus(Integer id, Integer status, Integer shopId) {
        CmsInfo bean = cmsInfoMng.findById(id);
        bean.setStatus(status.shortValue());
        bean.setUpdateTime(new Timestamp(DateTime.now().getMillis()));
        cmsInfoMng.update(bean, shopId);
        return "redirect:v_list.do";
    }

    private String getPageHeadURL(Integer shopId) {
        ShopInfo shopInfo = wiShopInfoMng.findById(shopId);
        return shopInfo.getDetailPageURL();
    }

    @Autowired
    private ProductMng productMng;

    @Autowired
    private WiShopInfoMng shopMng;

    @Autowired
    private CmsInfoMng cmsInfoMng;

    @Autowired
    private GroupBuyMng groupBuyMng;

    @Autowired
    private KillBuyMng killBuyMng;

    @Autowired
    private VIPProductMng vipProductMng;

    @Autowired
    private WiShopInfoMng wiShopInfoMng;
}
